<?php
header('Access-Control-Allow-Origin:*');

/**
 * 松鼠屋接口
 */
class Shipping
{//类定义开始

    /**
     * 开发者账号中的app_key
     * @var string
     */
    protected $AppKey = '';

    /**
     * 开发者账号中的app_secret秘钥
     * @var string
     */
    protected $AppSecret = '';

    /**
     * API版本 固定1
     * @var int
     */
    protected $Version = 1;

    /**
     * 业务参数，JSON字符串
     * @var string
     */
    protected $Params = null;

    /**
     * 接口访问地址
     * @var string
     */
    protected $ApiUrl = 'https://open.maiyatian.com';
   // protected $ApiUrl = 'http://open.test.maiyatian.com';

    /**
     * 构造方法，配置应用信息
     * @param array
     */
    public function __construct()
    {
//        $this->AppKey = '825914';
//        $this->AppSecret = '4f71a98dcd25a2619ee79e901c389f7b';
        $this->AppKey = '567190';
        $this->AppSecret = '5cd12711be19746afa7bde053b77bd71';
    }


    /**
     * 订单查询  可调用此接口查询不同状态下的订单列表
     * @param $CUrl string 请求访问接口方法 "/order/query/"
     * @param $Params array
     */
    public function GetMaiYaTianData($CUrl, $Params)
    {
        $ApiUrl = $this->ApiUrl . $CUrl;
        $DataParams = $this->GetParams($Params);
        $DataParams = json_encode($DataParams, true);
        $token_data = $this->curl_POST($DataParams, $ApiUrl);
        $token_data = json_decode($token_data, true);
        return $token_data;

    }


    /**
     * 获取Sign参数借口
     */
    public function GetParams($Params)
    {
        $TimeStamp = time();
        $DataParams = array(
            "app_key" => $this->AppKey,
            "timestamp" => $TimeStamp,
            "version" => $this->Version,
            "params" => json_encode($Params),
        );
        $Sign = $this->GetSign($DataParams);
        $DataParams['sign'] = $Sign;
        return $DataParams;
    }

    /**
     * 获取参数签名；
     * @param Array  要传递的参数数组
     * @return String 通过计算得到的签名；
     */
    function GetSign($params)
    {
        //签名步骤一：将参与签名的参数按照键值(key)进行字典排序
        // foreach($params as $k => $v){
        //         $Parameters[$k] = $v;
        //     }
        //     ksort($Parameters);
        //签名步骤二：将排序过后的参数 进行key和value字符串拼接
        $String = $this->formatBizQueryParaMap($params);
        // var_dump($String);
        //签名步骤三：在string后加入AppSecret
        $String = $this->AppSecret . $String . $this->AppSecret;
        //签名步骤四：MD5加密
        $String = md5($String);
        //签名步骤五：所有字符转为大写
        $result = strtoupper($String);
//        exit;
        return $result;
    }

    /**
     * 格式化参数（签名过程需要）
     * @param array $paraMap
     * @return bool|string
     */
    public function formatBizQueryParaMap($paraMap)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            $buff .= $k . $v;
        }
        return $buff;
    }

    /**
     * POST提交
     * @param string $data 提交的数据
     * @param string $url 地址
     * @param int $second 超时时间
     * @return bool|mixed
     */
    public function curl_POST($data, $url, $second = 30)
    {
        //初始化curl
        $ch = curl_init();
        $Header = array("Content-Type: application/json; charset=UTF-8");
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        //运行curl
        $res = curl_exec($ch);
        //返回结果
        if ($res) {
            curl_close($ch);
            return $res;
        } else {
            $error = curl_errno($ch);
            echo "curl出错，错误码:$error" . "<br>";
            curl_close($ch);
            return false;
        }
    }

    public function executeShipping($url, $param)
    {
//        $url = $_POST['url'];
//        $param = json_decode($_POST['param']);
        $res = $this->GetMaiYaTianData($url, $param);
        return json_encode($res);
    }

    function unicodeDecode($unicode_str)
    {
        $json = '{"str":"' . $unicode_str . '"}';
        $arr = json_decode($json, true);
        if (empty($arr)) return '';
        return $arr['str'];
    }


}
$url = empty($_POST['url']) ? '' : $_POST['url'];
if(!empty($url)){
    $class = new Shipping();
    $param = $_POST['param'];
    $res = $class->executeShipping($url, $param);
    exit(json_encode($res));
}
